package icu.feigroup.domain.activity.service.discount;

import icu.feigroup.domain.activity.adapter.repository.IActivityRepository;
import icu.feigroup.domain.activity.model.valobj.DiscountTypeEnum;
import icu.feigroup.domain.activity.model.valobj.GroupActivityDiscountVO;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

/**
 * projectName:icu.feigroup.domain.activity.service.discount
 *
 * @author 飞
 * @since 2025/2/20 10:20
 * description:
 */
@Slf4j
public abstract class AbstractDiscountCalculateService implements IDiscountCalculateService {
    private IActivityRepository repository;
    @Override
    public BigDecimal calculate(String userId, BigDecimal originPrice, GroupActivityDiscountVO.GroupDiscount groupDiscount) {
        if (DiscountTypeEnum.TAG.equals(groupDiscount.getDiscountType())){
            boolean isCrowdRare = filterTagId(userId,groupDiscount.getTagId());
            if (!isCrowdRare) {
                log.info("折扣优惠计算拦截，用户不再优惠人群标签范围内 userId:{}", userId);
                return originPrice;
            }
        }
        return doCalculate(originPrice,groupDiscount);
    }

    private boolean filterTagId(String userId, String tagId) {
        return repository.isTagCrowdRange(userId,tagId);
    }

    protected abstract BigDecimal doCalculate(BigDecimal originPrice, GroupActivityDiscountVO.GroupDiscount groupDiscount);

    protected BigDecimal compareZero(BigDecimal deductionPrice){
        return deductionPrice.compareTo(BigDecimal.ZERO)<=0 ? new BigDecimal("0.01") : deductionPrice;
    }
}
